﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace yysAuxiliary.FS
{
    /// <summary>
    /// 御魂
    /// </summary>
    public class YH : BaseFs
    {

        protected override string prefix => "御魂_";
        public YH(Action<string> SendMsg) : base(SendMsg)
        {
        }

        /// <summary>
        /// 去御魂界面
        /// </summary>
        /// <returns></returns>
        private async Task GoYH()
        {
            await Task.Delay(1000);
            var result = OperationPoint.WhileCheck(10, 1000, OperationPoint.OperationType.YuHun, 5);
            if (!result)
            {
                SendMsg("未找到御魂按钮，退出任务");
                return;
            }
            await GoBQDS();
        }

        /// <summary>
        /// 去八岐大蛇
        /// </summary>
        /// <returns></returns>
        private async Task GoBQDS()
        {
            await Task.Delay(1000);
            var result = OperationPoint.WhileCheck(10, 1000, OperationPoint.OperationType.BQDS, 25);
            if (!result)
            {
                SendMsg("没有找到八岐大蛇入口，退出任务");
                return;
            }
            await GoBeiMing();
        }

        /// <summary>
        /// 去打悲鸣
        /// </summary>
        /// <returns></returns>
        private async Task GoBeiMing()
        {
            await Task.Delay(1000);
            var result = OperationPoint.WhileCheck(10, 1000, OperationPoint.OperationType.BeiMing, 0);
            if (!result)
            {
                SendMsg("没有找到悲鸣入口，退出任务");
                return;
            }
            await Execute();
        }

        /// <summary>
        /// 御魂
        /// </summary>
        public override async Task Run()
        {
            await GetValidGameInterface();
            switch (Data.IndexGameInterface)
            {
                case Data.GameInterface.Main:
                    if (await GoTS())
                    {
                        await GoYH();
                    }
                    else
                    {
                        SendMsg("无法去探索界面，退出任务");
                    }
                    break;
                case Data.GameInterface.TS:
                    await GoYH();
                    break;
                default:
                    SendMsg("不支持的界面操作，退出任务");
                    break;
            }
            GetNextFS();
        }

        /// <summary>
        /// 循环执行
        /// </summary>
        private async Task Execute()
        {
            int i = 0;
            var zsw = new Stopwatch();
            var sw = new Stopwatch();
            while (i < Data.IndexFS.Count && Data.isRun)
            {
                zsw.Start();
                sw.Restart();
                SendMsg($"执行第{i + 1}遍开始");
                var result = OperationPoint.WhileCheck(5, 1000, OperationPoint.OperationType.TiaoZhan, 10);
                if (!result)
                {
                    SendMsg($"没找到挑战,结束御魂执行");
                    return;
                }
                await Task.Delay(25000);
                while (Data.isRun)
                {
                    result = OperationPoint.ExisitWithCheck(OperationPoint.OperationType.JS, 20);
                    if (!result)
                    {
                        SendMsg($"还没有结束");
                        GetIndexGameInterface();
                        await Task.Delay(1000);
                    }
                    else
                    {
                        while (Data.isRun)
                        {
                            //防止结算出来过早没有点击出发成功就结束了，因此要判断到当前的界面没有结算状态才可以
                            result = OperationPoint.ExisitWithCheck(OperationPoint.OperationType.JS);
                            if (result)
                            {
                                await Task.Delay(300);
                            }
                            else
                                break;
                        }
                        break;
                    }
                }
                i++;
                sw.Stop();
                zsw.Stop();
                SendMsg($"执行第{i}遍结束,总任务耗时{zsw.Elapsed.TotalSeconds}，单次耗时{sw.Elapsed.TotalSeconds}");
                await Task.Delay(1000);
            }
        }
    }
}
